package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionContext;
import com.printeverywhere.delegator.AdminDetailsServiceDelegator;
import com.printeverywhere.delegator.AdminServiceDelegator;
import com.printeverywhere.delegator.LocationCategoriesServiceDelegator;
import com.printeverywhere.delegator.MiscellaneousFeesServiceDelegator;
import com.printeverywhere.dto.AdminDetails;
import com.printeverywhere.dto.LocationCategories;
import com.printeverywhere.dto.MiscellaneousFees;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.SearchListCriteria;

public class AdminDetailsAction extends AdminSystemSetup{
	Logger logger = Logger.getLogger(AdminDetailsAction.class);
	private AdminDetails adminDetails;
	private List<LocationCategories> tempList = new ArrayList<LocationCategories>();
	private List<LocationCategories> countryList = new ArrayList<LocationCategories>();
	private List<LocationCategories> stateList = new ArrayList<LocationCategories>();
	SearchListCriteria searchListCriteria = new SearchListCriteria();
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	public String execute() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}
		try{
			getOwnerDetails();
		}catch (Exception e) {

		}
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public String updateOwnerDetails(){
		try {
			if(adminDetails.getVatAmtPercentage().contains("%")){
				adminDetails.getVatAmtPercentage().replaceAll("%", AdminConstants.NONE);
			}
			AdminDetailsServiceDelegator.updateAdminDetails(adminDetails);
			addActionMessage("Owner details updated");
		} catch (Exception e) {
			getOwnerDetails();
			return INPUT;
		}
		return SUCCESS;
	}
	public void getOwnerDetails(){
		adminDetails = AdminDetailsServiceDelegator.getAdminDetails();
		countryList = getLocation(countryList, AdminConstants.TWO_NUMBER, AdminConstants.ZERO_NUMBER);
		if(adminDetails.getCountry() != null) {
			stateList = getLocation(stateList, AdminConstants.THREE_NUMBER, adminDetails.getCountry());
		}
	}
	public List<LocationCategories> getLocation(List<LocationCategories> list, int level, int categoryId){
		tempList = (List<LocationCategories>) ActionContext.getContext().getApplication().get(AdminConstants.LOCATIONCATEGORIES);
		list.clear();
		for (Iterator<LocationCategories> iterator = tempList.iterator(); iterator
				.hasNext();) {
			LocationCategories locationCategories = (LocationCategories) iterator
					.next();

				if(categoryId > 0){
					if(locationCategories.getLocationCategories().getCategoryId() == categoryId){
						if (locationCategories.getLevel() == level){
							list.add(locationCategories);
						}
					}
				}else {
					if (locationCategories.getLevel() == level){
						list.add(locationCategories);
					}
				}
		}
		return list;
	}
	public List<LocationCategories> getCountryList() {
		return countryList;
	}
	public void setCountryList(List<LocationCategories> countryList) {
		this.countryList = countryList;
	}

	public AdminDetails getAdminDetails() {
		return adminDetails;
	}

	public void setAdminDetails(AdminDetails adminDetails) {
		this.adminDetails = adminDetails;
	}

	public List<LocationCategories> getStateList() {
		return stateList;
	}

	public void setStateList(List<LocationCategories> stateList) {
		this.stateList = stateList;
	}

}
